//
//  WDAdStatusManager.m
//  WDPolymerizeSDK
//
//  Created by little People on 2023/6/1.
//

#import "WDAdStatusManager.h"
#import "WDThinkingManager.h"
#import "WDCallBackModel.h"
#import "WDJsonManager.h"
#import "Adjust.h"

@implementation WDAdStatusManager
{
    NSString *splashLoadTimeSp;
}

static WDAdStatusManager *_status = nil;
+ (instancetype)shareStatusManager
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _status = [[self alloc] init];
        
    });
    return _status;
}



- (NSString *)eventNameString
{
    NSString *event;
    switch ([WDCallBackModel shareCallBackModel].adSpace) {
        case inter:
            event = @"AD_Inter";
            break;
        case reward:
            event = @"AD_RV";
            break;
        case banner:
            event = @"AD_Banner";
            break;
        case splash:
            event = @"AD_Splash";
            break;
        case native:
            event = @"AD_Native";
            break;
        case card:
            event = @"AD_CARD";
            break;
            
        default:
            break;
    }
    return event;
}



- (void)adRequestWithEventName:(NSString *)eventName
{
    
    [[WDThinkingManager shareThinkingManager] thinkingTrackEventWithKey:[NSString stringWithFormat:@"%@_request",[self eventNameString]] jsonStr:@{@"w_request":@""}];
}

- (void)adLoadWithEventName:(NSString *)eventName
{
    splashLoadTimeSp = [NSString stringWithFormat:@"%ld", (long)[[NSDate date]timeIntervalSince1970]];
    [[WDThinkingManager shareThinkingManager] thinkingTrackEventWithKey:[NSString stringWithFormat:@"%@_load",eventName] jsonStr:@{@"w_load":@""}];
    WDLog(@"ad开始加载时间：%@",splashLoadTimeSp);
}

- (void)adLoadFailedWithCode:(NSInteger)code message:(NSString *)message eventName:(NSString *)eventName
{
    [[WDThinkingManager shareThinkingManager] thinkingTrackEventWithKey:[NSString stringWithFormat:@"%@_loadfailed",eventName] jsonStr:@{@"w_error_code":[NSString stringWithFormat:@"%ld",code],@"w_error_msg":message}];
    WDLog(@"ad填充失败error code:%ld",code);
}

- (void)adLoadFillSuccessWithEventName:(NSString *)eventName
{
    NSString *fillSuccessTimeSp = [NSString stringWithFormat:@"%ld", (long)[[NSDate date]timeIntervalSince1970]];
    [[WDThinkingManager shareThinkingManager] thinkingTrackEventWithKey:[NSString stringWithFormat:@"%@_fill",eventName] jsonStr:@{@"w_fill":[NSString stringWithFormat:@"%ld",[fillSuccessTimeSp integerValue]-[splashLoadTimeSp integerValue]]}];
    WDLog(@"ad填充成功时间：%@",fillSuccessTimeSp);
    WDLog(@"ad开始load到填充成功的时间：%ld",[fillSuccessTimeSp integerValue]-[splashLoadTimeSp integerValue]);
}

- (void)adLoadFillFailedWithEventName:(NSString *)eventName
{
    NSString *fillFailedTimeSp = [NSString stringWithFormat:@"%ld", (long)[[NSDate date]timeIntervalSince1970]];
    [[WDThinkingManager shareThinkingManager] thinkingTrackEventWithKey:[NSString stringWithFormat:@"%@_fillfailed",eventName] jsonStr:@{@"w_fillfailed":[NSString stringWithFormat:@"%ld",[fillFailedTimeSp integerValue]-[splashLoadTimeSp integerValue]]}];
    WDLog(@"ad填充失败时间：%@",fillFailedTimeSp);
    WDLog(@"ad开始load到填充失败的时间：%ld",[fillFailedTimeSp integerValue]-[splashLoadTimeSp integerValue]);
}

- (void)adShowWithEventName:(NSString *)eventName
{
    //本次实际展示的广告类型：1-reward；2-inter；3-banner；4-splash；5-native
    NSString *type;
    if ([eventName isEqualToString:RV_EVENT_NAME])
    {
        type = @"1";
    }
    if ([eventName isEqualToString:INTER_EVENT_NAME])
    {
        type = @"2";
    }
    if ([eventName isEqualToString:BANNER_EVENT_NAME])
    {
        type = @"3";
    }
    if ([eventName isEqualToString:SPLASH_EVENT_NAME])
    {
        type = @"4";
    }
    if ([eventName isEqualToString:NATIVE_EVENT_NAME])
    {
        type = @"5";
    }
    if ([eventName isEqualToString:CARD_EVENT_NAME])
    {
        type = @"6";
    }

    [[WDThinkingManager shareThinkingManager] thinkingTrackEventWithKey:[NSString stringWithFormat:@"%@_show",eventName]   jsonStr:@{@"w_show":type}];
    WDLog(@"ad开始展示回调--------%@--------%@",type,eventName);
}

- (void)adShowFailedWithCode:(NSInteger)code message:(NSString *)message eventName:(NSString *)eventName
{
    [[WDThinkingManager shareThinkingManager] thinkingTrackEventWithKey:[NSString stringWithFormat:@"%@_showfailed",eventName] jsonStr:@{@"w_error_code":[NSString stringWithFormat:@"%ld",code],@"w_error_msg":message}];
    WDLog(@"ad展示失败error code:%ld",code);
}

- (void)adCompletedWithEventName:(NSString *)eventName
{
    [[WDThinkingManager shareThinkingManager] thinkingTrackEventWithKey:[NSString stringWithFormat:@"%@_completed",eventName] jsonStr:@{@"w_show":@""}];
    WDLog(@"ad展示完成");
}

- (void)adClickWithEventName:(NSString *)eventName
{
    [[WDThinkingManager shareThinkingManager] thinkingTrackEventWithKey:[NSString stringWithFormat:@"%@_click",eventName] jsonStr:@{@"w_click":@""}];
    WDLog(@"ad点击");
}

- (void)adCloseWithEventName:(NSString *)eventName
{
    [[WDThinkingManager shareThinkingManager] thinkingTrackEventWithKey:[NSString stringWithFormat:@"%@_close",eventName] jsonStr:@{@"w_close":@""}];
    WDLog(@"ad关闭");
}

- (void)adRevenue:(NSString *)type source:(NSString *)source revenue:(NSNumber *)revenue currency:(NSString *)currency
{
    NSDictionary *json = @{@"w_type":type,
                           @"w_source":source,
                           @"w_revenue":revenue,
                           @"w_currency":currency
    };
    WDLog(@"--RV---%@",json);
    
    [[WDThinkingManager shareThinkingManager] thinkingTrackEventWithKey:REVENUE_EVENT_NAME jsonStr:json];
}

- (void)customAdShowWithEventName:(NSString *)eventName package:(NSString *)package creative:(NSString *)creative
{
    [[WDThinkingManager shareThinkingManager] thinkingTrackEventWithKey:[NSString stringWithFormat:@"%@_show",eventName] jsonStr:@{@"w_package":package,@"w_creative":creative}];
}

- (void)customAdClickWithEventName:(NSString *)eventName package:(NSString *)package creative:(NSString *)creative
{
    [[WDThinkingManager shareThinkingManager] thinkingTrackEventWithKey:[NSString stringWithFormat:@"%@_show",eventName] jsonStr:@{@"w_package":package,@"w_creative":creative}];
}

- (void)cardAdClickWithEventName:(NSString *)eventName adType:(NSString *)type
{
    NSString *ad;
    if ([type isEqualToString:@"native"])
    {
        ad = @"5";
    }
    else if ([type isEqualToString:@"自推广"])
    {
        ad = @"6";
    }
    [[WDThinkingManager shareThinkingManager] thinkingTrackEventWithKey:[NSString stringWithFormat:@"%@_click",eventName] jsonStr:@{@"w_show":ad}];
}

//type:本次实际展示的广告类型 5-native 6-自推广
- (void)cardAdShowWithEventName:(NSString *)eventName adType:(NSString *)type
{
    WDLog(@"card ad开始展示");
    NSString *ad;
    if ([type isEqualToString:@"native"])
    {
        ad = @"5";
    }
    else if ([type isEqualToString:@"自推广"])
    {
        ad = @"6";
    }
    [[WDThinkingManager shareThinkingManager] thinkingTrackEventWithKey:[NSString stringWithFormat:@"%@_show",eventName]   jsonStr:@{@"w_show":ad}];
}

- (void)inAppPurchaseRequestWithEvent:(NSString *)eventName productId:(NSString *)productId
{
    [[WDThinkingManager shareThinkingManager] thinkingTrackEventWithKey:[NSString stringWithFormat:@"%@_purchase_request",eventName] jsonStr:@{@"w_product_id":productId}];
}

- (void)inAppPurchaseSuccessWithEvent:(NSString *)eventName productId:(NSString *)productId
{
    [[WDThinkingManager shareThinkingManager] thinkingTrackEventWithKey:[NSString stringWithFormat:@"%@_purchase_success",eventName] jsonStr:@{@"w_product_id":productId}];
}

- (void)inAppPurchaseFailedWithEvent:(NSString *)eventName productId:(NSString *)productId errorType:(NSString *)type errorMsg:(NSString *)msg
{
    
    [[WDThinkingManager shareThinkingManager] thinkingTrackEventWithKey:[NSString stringWithFormat:@"%@_purchase_failed",eventName] jsonStr:@{@"w_product_id":productId,@"w_error_type":type,@"w_error_msg":msg}];
}

- (void)inAppPurchaseRevenueWithEvent:(NSString *)eventName type:(NSString *)type source:(NSString *)source d_revenue:(NSNumber *)d_revenue currency:(NSString *)currency
{
    NSDictionary * thinkString = @{@"w_type":type,@"w_source":source,@"w_d_revenue":d_revenue,@"w_currency":currency};
    
    
    WDLog(@"thinkString--------%@",thinkString);
    [[WDThinkingManager shareThinkingManager] thinkingTrackEventWithKey:[NSString stringWithFormat:@"%@_Revenue",eventName] jsonStr:thinkString];

}



@end
